use serde_derive::Deserialize;
use std::{fs::File, io::Read};

#[derive(Deserialize)]
pub struct Config {
    pub settings: Settings,
    pub database: Database,
}
#[derive(Deserialize)]
pub struct Settings {
    pub title: String,
    pub port: u32,
}

#[derive(Deserialize)]
pub struct Database {
    pub url: String,
    pub user: String,
    pub password: String,
}

// 读取配置文件，把参数信息存入 struct Config 
pub fn read_config_file() -> Config {
    let config_file_path = "config/config.toml";
    let mut file = match File::open(config_file_path) {
        Ok(f) => f,
        Err(_) => panic!("配置文件[{}]不存在！", config_file_path),
    };

    let mut str_val = String::new();
    match file.read_to_string(&mut str_val) {
        Ok(s) => s,
        Err(_) => panic!("读取文件内容失败！"),
    };

    println!("{}", str_val);

    let config: Config = toml::from_str(&str_val).unwrap();

    println!("{}", config.database.url);

    config
} 